package com.yc.ui.admin;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.Idao.LoginIDao;
import com.yc.dao.LoginDao;
import com.yc.ui.Order;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class AdminRoom extends Composite {


	private static List<Map<String, Object>> list; // 房间信息
	private static Map<String, Map<String, Object>> map2 = new HashMap<String, Map<String,Object>>();
	private static Map<String, Label> mapRoom = new HashMap<String, Label>();

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public AdminRoom(Composite parent, int style) {
		super(parent, style);
		this.setLayout(new FillLayout(SWT.HORIZONTAL));

		SashForm sashForm_1 = new SashForm(this, SWT.NONE);

		LoginIDao dao = new LoginDao();
		this.list = dao.DemandRoom();

		Composite com_right = new Composite(sashForm_1, SWT.BORDER | SWT.V_SCROLL);
		com_right.setLayout(null);
		com_right.setBackgroundImage( SWTResourceManager.getImage(Order.class, "/com/yc/images/客房预约1.PNG") );
		SWTUtil.imagetSize(com_right);
		Label lab_1 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("101".equals(map3.get("r_id").toString())) {
				map2.put("101", map3);
				mapRoom.put("101", lab_1);
			}
		}
		lab_1.setText(Roomtext(lab_1, "101"));
		lab_1.setBounds(10, 43, 121, 121);

		Label lab_2 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("102".equals(map3.get("r_id").toString())) {
				map2.put("102", map3);
				mapRoom.put("102", lab_2);
			}
		}
		lab_2.setText(Roomtext(lab_2, "102"));
		lab_2.setBounds(184, 43, 121, 121);

		Label lab_3 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("103".equals(map3.get("r_id").toString())) {
				map2.put("103", map3);
				mapRoom.put("103", lab_3);
			}
		}
		lab_3.setText(Roomtext(lab_3, "103"));
		lab_3.setBounds(356, 43, 121, 121);

		Label lab_4 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("104".equals(map3.get("r_id").toString())) {
				map2.put("104", map3);
				mapRoom.put("104", lab_4);
			}
		}
		lab_4.setText(Roomtext(lab_4, "104"));
		lab_4.setBounds(533, 43, 121, 121);

		Label lab_5 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("105".equals(map3.get("r_id").toString())) {
				map2.put("105", map3);
				mapRoom.put("105", lab_5);
			}
		}
		lab_5.setText(Roomtext(lab_5, "105"));
		lab_5.setBounds(727, 43, 121, 121);

		Label lab_6 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("106".equals(map3.get("r_id").toString())) {
				map2.put("106", map3);
				mapRoom.put("106", lab_6);
			}
		}
		lab_6.setText(Roomtext(lab_6, "106"));
		lab_6.setBounds(904, 43, 121, 121);

		Label lab_7 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("201".equals(map3.get("r_id").toString())) {
				map2.put("201", map3);
				mapRoom.put("201", lab_7);
			}
		}
		lab_7.setText(Roomtext(lab_7, "201"));
		lab_7.setBounds(10, 182, 164, 121);

		Label lab_8 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("202".equals(map3.get("r_id").toString())) {
				map2.put("202", map3);
				mapRoom.put("202", lab_8);
			}
		}
		lab_8.setText(Roomtext(lab_8, "202"));
		lab_8.setBounds(284, 182, 164, 121);

		Label lab_9 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("203".equals(map3.get("r_id").toString())) {
				map2.put("203", map3);
				mapRoom.put("203", lab_9);
			}
		}
		lab_9.setText(Roomtext(lab_9, "203"));
		lab_9.setBounds(562, 182, 164, 121);

		Label lab_10 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("204".equals(map3.get("r_id").toString())) {
				map2.put("204", map3);
				mapRoom.put("204", lab_10);
			}
		}
		lab_10.setText(Roomtext(lab_10, "204"));
		lab_10.setBounds(854, 182, 171, 121);

		Label lab_1_10 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("301".equals(map3.get("r_id").toString())) {
				map2.put("301", map3);
				mapRoom.put("301", lab_1_10);
			}
		}
		lab_1_10.setText(Roomtext(lab_1_10, "301"));
		lab_1_10.setBounds(10, 318, 150, 121);

		Label lab_1_11 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("302".equals(map3.get("r_id").toString())) {
				map2.put("302", map3);
				mapRoom.put("302", lab_1_11);
			}
				
		}
		lab_1_11.setText(Roomtext(lab_1_11, "302"));
		lab_1_11.setBounds(229, 318, 150, 121);

		Label lab_1_12 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("303".equals(map3.get("r_id").toString())) {
				map2.put("303", map3);
				mapRoom.put("303", lab_1_12);
			}
		}
		lab_1_12.setText(Roomtext(lab_1_12, "303"));
		lab_1_12.setBounds(438, 318, 150, 121);

		Label lab_1_13 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("304".equals(map3.get("r_id").toString())) {
				map2.put("304", map3);
				mapRoom.put("304", lab_1_13);
			}
		}
		lab_1_13.setText(Roomtext(lab_1_13, "304"));
		lab_1_13.setBounds(648, 318, 164, 121);

		Label lab_1_14 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("305".equals(map3.get("r_id").toString())) {
				map2.put("305", map3);
				mapRoom.put("305", lab_1_14);
			}
		}
		lab_1_14.setText(Roomtext(lab_1_14, "305"));
		lab_1_14.setBounds(887, 318, 138, 121);


		Label lab_2_1 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("401".equals(map3.get("r_id").toString())) {
				map2.put("401", map3);
				mapRoom.put("401", lab_2_1);
			}
		}
		lab_2_1.setText(Roomtext(lab_2_1, "401"));
		lab_2_1.setBounds(10, 461, 270, 121);

		Label lab_2_2 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("402".equals(map3.get("r_id").toString())) {
				map2.put("402", map3);
				mapRoom.put("402", lab_2_2);
			}
			
		}
		lab_2_2.setText(Roomtext(lab_2_2, "402"));
		lab_2_2.setBounds(372, 461, 282, 121);

		Label lab_2_3 = new Label(com_right, SWT.CENTER);
		for(Map<String, Object> map3 : list) {
			if("403".equals(map3.get("r_id").toString())) {
				map2.put("403", map3);
				mapRoom.put("403", lab_2_3);
			}
				
		}
		lab_2_3.setText(Roomtext(lab_2_3, "403"));
		lab_2_3.setBounds(774, 461, 261, 121);

		Label lbll_1 = new Label(com_right, SWT.NONE);
		lbll_1.setText("在住");
		lbll_1.setBounds(372, 10, 76, 20);

		Label label = new Label(com_right, SWT.NONE);
		label.setBounds(69, 10, 76, 20);
		label.setText("空闲");

		Label lbll = new Label(com_right, SWT.NONE);
		lbll.setBounds(229, 10, 76, 20);
		lbll.setText("打扫");

		Label lblNewLabel = new Label(com_right, SWT.NONE);
		lblNewLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
		lblNewLabel.setBounds(23, 10, 22, 20);

		Label lblNewLabel_1 = new Label(com_right, SWT.NONE);
		lblNewLabel_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		lblNewLabel_1.setBounds(175, 10, 22, 20);

		Label lblNewLabel_2 = new Label(com_right, SWT.NONE);
		lblNewLabel_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblNewLabel_2.setBounds(332, 10, 22, 20);
		sashForm_1.setWeights(new int[] {851});
	}

	public String Roomtext(Label lab, String r_id){
		String r = "";
		r += r_id + "\n" + map2.get(r_id).get("r_type");
		if("2".equals(map2.get(r_id).get("r_status").toString())) {
			lab.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseDoubleClick(MouseEvent e) {
					MessageBox messageBox = new MessageBox(getShell(), SWT.OK | SWT.CANCEL);
					messageBox.setMessage("房间是否已经打扫好？");
					if(messageBox.open() == 32) {
						LoginIDao dao = new LoginDao();
						int re = 0;
						re = dao.UpdateRoom(r_id);
						if(re == 0) {
							StringUtil.showMessageBox(getShell(), "打扫失败");
							return;
						}else {
							StringUtil.showMessageBox(getShell(), "打扫成功");
							Color(r_id);
						}
					}else {
						return ;
					}
				}
			});
		}
		Color(r_id);
		return r;
	}

	// 设置颜色
	public static void Color(String r_id) {
		LoginIDao dao = new LoginDao();
		Map<String, Object> map = dao.SelectR_status(r_id);
		if(Integer.parseInt(map.get("r_status").toString()) == 1) {
			mapRoom.get(r_id).setBackground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
		}else if(Integer.parseInt(map.get("r_status").toString()) == 2) {
			mapRoom.get(r_id).setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		}else {
			mapRoom.get(r_id).setBackground(SWTResourceManager.getColor(SWT.COLOR_RED));
		}
	}
}
